//
//  LZNotificationPresenter.m
//  laziz_Consumer
//
//  Created by xbn_iMac on 2016/12/16.
//  Copyright © 2016年 XBN. All rights reserved.
//

#import "LZNotificationPresenter.h"
#import "JPUSHService.h"

#import "LZRestaurantAuthenticationViewController.h"
#import "LZManagePictureContentViewController.h"
#import "LZFinancialDetailViewController.h"
#import "LZMsgCenterCommentViewController.h"
#import "GVUserDefaults+LZProperties.h"
#import "LZAlertView.h"
#import "LZMsgCenterPresenter.h"
#import "LZHomeViewController.h"
#import "LZMyMessageCenterViewController.h"

@interface LZNotificationPresenter ()
@end

@implementation LZNotificationPresenter

#pragma mark - public method

// 设置别名
+(void)setNotificationAlias:(NSString*)alias {
    if (alias.length>0) {
        [JPUSHService setAlias:alias callbackSelector:nil object:nil];
    }
}

// 移除别名
+(void)removeNotificationAlias {
    [JPUSHService setAlias:@"" callbackSelector:nil object:nil];
}


/**
 设置新的JPush 服务器角标数

 @param count <#count description#>
 */
+ (void)setNewBadgeCount:(NSInteger)count
{
    [JPUSHService setBadge:count];
}


#pragma mark - 处理推送消息.当app 在前台的时候
- (void)dealNotificationWhenAPPInDeskWithInfo:(NSDictionary *)dic
{
    UINavigationController *nav = (UINavigationController*)[UIApplication sharedApplication].delegate.window.rootViewController;
    UIViewController *lastVC = nav.viewControllers.lastObject;
    
    if ([lastVC isKindOfClass:[LZHomeViewController class]]) {
        
        [(LZHomeViewController *)lastVC checkUnReadMsgCount];
        
    }else if ([lastVC isKindOfClass:[LZMyMessageCenterViewController class]]){
        
        [(LZMyMessageCenterViewController *)lastVC downLoadNewData];
    }
}

#pragma mark - 处理apns 推送
- (void)dealNotificationWithDictionary:(NSDictionary *)notificationDic
{
    UINavigationController *nav = (UINavigationController*)[UIApplication sharedApplication].delegate.window.rootViewController;

    NSString *alertStr = notificationDic[@"aps"][@"alert"];
    NSString *contentStr = notificationDic[@"bussinessId"];
    NSInteger typeTemp = [notificationDic[@"type"] integerValue];
    NSString *msgId = notificationDic[@"msgId"];
    [self changeMsgIsReaded:msgId];
    
    if (typeTemp == kMsgCenterType_videoComment) {//视频评论
        
        LZMsgCenterCommentViewController *vc = [[LZMsgCenterCommentViewController alloc] initWithNibName:@"LZMsgCenterCommentViewController" bundle:nil];
        vc.reviewType = ReviewType_Video;
        vc.commentId = contentStr;
        [nav pushViewController:vc animated:YES];
        
    }else if (typeTemp == kMSgCenterType_resComment){//餐厅评论
        
        LZMsgCenterCommentViewController *vc = [[LZMsgCenterCommentViewController alloc] initWithNibName:@"LZMsgCenterCommentViewController" bundle:nil];
        vc.commentId = contentStr;
        vc.reviewType = ReviewType_Restaurants;
        [nav pushViewController:vc animated:YES];
    
    }else if (typeTemp == kMsgCenterType_resAuthRegected){//认证被拒
        if ([GVUserDefaults standardUserDefaults].authType != kAuthType_completeAuth) {
            LZRestaurantAuthenticationViewController *vc = [[LZRestaurantAuthenticationViewController alloc] initWithNibName:@"LZRestaurantAuthenticationViewController" bundle:nil];
            vc.regected = YES;
            [nav pushViewController:vc animated:YES];
        }
        
    }else if (typeTemp == kMsgCenterType_resAuthSuccess){//认证通过
        
        if ([GVUserDefaults standardUserDefaults].authType != kAuthType_completeAuth) {
            [GVUserDefaults standardUserDefaults].authType = kAuthType_completeAuth;
            LZAlertView *alert = [[LZAlertView alloc] initWithContent:LZLocalizedString(@"message_res_auth_success_exit", nil) andImage:nil cancelButtonTitle:nil otherButtonTitles:LZLocalizedString(@"confirm", nil),nil];
            
            [alert show];
            
            alert.clickButtonIndexBlock = ^(NSInteger index) {
                exit(0);
            };
        }
        
    }else if (typeTemp == kMsgCenterType_photosDelete){//删除违规图片
        
        LZManagePictureContentViewController *vc = [[LZManagePictureContentViewController alloc]initWithNibName:@"LZManagePictureContentViewController" bundle:nil];
        vc.isCurrentVCNotChildVC = YES;
        vc.delBatch = contentStr;
        vc.deleteReason = alertStr;
        [nav pushViewController:vc animated:YES];
        
    }else if (typeTemp == kMsgCenterType_finicalRecode){//平台结账通知
        
        LZFinancialDetailViewController *vc = [[LZFinancialDetailViewController alloc] initWithNibName:@"LZFinancialDetailViewController" bundle:nil];
        vc.payID = contentStr;
        [nav pushViewController:vc animated:YES];
        
    }


}

/**
 这里请求你信息详情.只是为了告知后台,此条消息已经读取.
 
 @param msgId <#msgId description#>
 */
- (void)changeMsgIsReaded:(NSString *)msgId
{
    if (msgId) {
        LZMsgCenterPresenter *p = [[LZMsgCenterPresenter alloc] init];
        __block NSInteger cc = [[UIApplication sharedApplication] applicationIconBadgeNumber];
        [p getMsgDetailWithMsgId:msgId Success:^(id data) {
            cc -- ;
//            [[UIApplication sharedApplication] setApplicationIconBadgeNumber:cc];
            [LZNotificationPresenter setNewBadgeCount:cc];
            
        } Failure:^(NSString *fail) {
            
        }];
    }
   
}

@end
